第二部分 第十章 3.ArrayList类
条评论10.3 ArrayList类
10.3.1 引入——对象数组
使用学生数组,存储三个学生对象,代码如下:
1 | public class Student { |
1 | public class Test01StudentArray { |
到目前为止,我们想存储对象数据,选择的容器,只有对象数组。而数组的长度是固定的,无法适应数据变化的需求。为了解决这个问题,Java提供了另一个容器 java.util.ArrayList
集合类,让我们可以更便捷的存储和操作对象数据。
10.3.2 什么是ArrayList类
查看类
java.util.ArrayList <E>
:该类需要 import导入使后使用。
<E>
,表示一种指定的数据类型,叫做泛型。E
,取自Element(元素)的首字母。在出现E
的地方,就使用一种引用数据类型将其替换即可,表示将存储哪种引用类型的元素。代码如下:
1
ArrayList<String>,ArrayList<Student>
查看构造方法
public ArrayList()
:构造一个内容为空的集合。基本格式:
1
ArrayList<String> list = new ArrayList<String>();
在JDK 7后,右侧泛型的尖括号之内可以留空,但是<>仍然要写。简化格式:
1
ArrayList<String> list = new ArrayList<>();
查看成员方法
public boolean add(E e)
: 将指定的元素添加到此集合的尾部。
参数E e
,在构造ArrayList
对象时,<E>
指定了什么数据类型,那么add(E e)
方法中,只能添加什么数据类型的对象。
使用ArrayList
类,存储三个字符串元素,代码如下:
1 | public class Test02StudentArrayList { |
10.3.3 常用方法和遍历
对于元素的操作,基本体现在——增、删、查。常用的方法有:
public boolean add(E e)
**:将指定的元素添加到此集合的尾部。public E remove(int index)
**:移除此集合中指定位置上的元素。返回被删除的元素。public E get(int index)
**:返回此集合中指定位置上的元素。返回获取的元素。public int size()
**:返回此集合中的元素数。遍历集合时,可以控制索引范围,防止越界。
这些都是最基本的方法,操作非常简单,代码如下:
1 | public class Demo01ArrayListMethod { |
10.3.4 如何存储基本数据类型
ArrayList对象不能存储基本类型,只能存储引用类型的数据。类似<int>
不能写,但是存储基本数据类型对应的包装类型是可以的。所以,想要存储基本类型数据,<>
中的数据类型,必须转换后才能编写,转换写法如下:
基本类型 | 基本类型包装类 |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
结果发现,只有Integer
和Character
需要特殊记忆,其他基本类型只是首字母大写即可。那么存储基本类型数据,代码如下:
1 | public class Demo02ArrayListMethod { |
10.3.5 ArrayList练习
数值添加到集合
生成6个1~33之间的随机整数,添加到集合,并遍历
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17public class Test01ArrayList {
public static void main(String[] args) {
// 创建Random 对象
Random random = new Random();
// 创建ArrayList 对象
ArrayList<Integer> list = new ArrayList<>();
// 添加随机数到集合
for (int i = 0; i < 6; i++) {
int r = random.nextInt(33) + 1;
list.add(r);
}
// 遍历集合输出
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
}
对象添加到集合
自定义4个学生对象,添加到集合,并遍历
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21public class Test02ArrayList {
public static void main(String[] args) {
//创建集合对象
ArrayList<Student> list = new ArrayList<Student>();
//创建学生对象
Student s1 = new Student("赵丽颖",18);
Student s2 = new Student("唐嫣",20);
Student s3 = new Student("景甜",25);
Student s4 = new Student("柳岩",19);
//把学生对象作为元素添加到集合中
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
//遍历集合
for(int x = 0; x < list.size(); x++) {
Student s = list.get(x);
System.out.println(s.getName()+"‐‐‐"+s.getAge());
}
}
}
打印集合方法
定义以指定格式打印集合的方法(ArrayList类型作为参数),使用{}扩起集合,使用@分隔每个元素。格式参照 {元素@元素@元素}。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29public class Test03ArrayList {
public static void main(String[] args) {
// 创建集合对象
ArrayList<String> list = new ArrayList<String>();
// 添加字符串到集合中
list.add("张三丰");
list.add("宋远桥");
list.add("张无忌");
list.add("殷梨亭");
// 调用方法
printArrayList(list);
}
public static void printArrayList(ArrayList<String> list) {
// 拼接左括号
System.out.print("{");
// 遍历集合
for (int i = 0; i < list.size(); i++) {
// 获取元素
String s = list.get(i);
// 拼接@符号
if (i != list.size() ‐ 1) {
System.out.print(s + "@");
} else {
// 拼接右括号
System.out.print(s + "}");
}
}
}
}
获取集合方法
定义获取所有偶数元素集合的方法(ArrayList类型作为返回值)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31public class Test04ArrayList {
public static void main(String[] args) {
// 创建Random 对象
Random random = new Random();
// 创建ArrayList 对象
ArrayList<Integer> list = new ArrayList<>();
// 添加随机数到集合
for (int i = 0; i < 20; i++) {
int r = random.nextInt(1000) + 1;
list.add(r);
}
// 调用偶数集合的方法
ArrayList<Integer> arrayList = getArrayList(list);
System.out.println(arrayList);
}
public static ArrayList<Integer> getArrayList(ArrayList<Integer> list) {
// 创建小集合,来保存偶数
ArrayList<Integer> smallList = new ArrayList<>();
// 遍历list
for (int i = 0; i < list.size(); i++) {
// 获取元素
Integer num = list.get(i);
// 判断为偶数,添加到小集合中
if (num % 2 == 0){
smallList.add(num);
}
}
// 返回小集合
return smallList;
}
}
本文标题:第二部分 第十章 3.ArrayList类
文章作者:foreverSFJ
发布时间:2019-08-19 11:51:11
最后更新:2019-08-19 11:51:11
原始链接:Notes/Java/Basic/Part02/10_3 ArrayList类.html
版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC-ND 4.0 许可协议。转载请注明出处!
分享